package com.imooc.o2o.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.imooc.o2o.dao.ReachPointDao;
import com.imooc.o2o.dto.ClassGradeExecution;
import com.imooc.o2o.dto.ExcelExecution;
import com.imooc.o2o.dto.ReachPointExecution;
import com.imooc.o2o.entity.ClassesGrade;
import com.imooc.o2o.entity.Excel;
import com.imooc.o2o.entity.ReachPoint;
import com.imooc.o2o.enums.ExcelStateEnum;
import com.imooc.o2o.enums.ReachPointStateEnum;
import com.imooc.o2o.enums.StudentStateEnum;
import com.imooc.o2o.excptions.ClassGradeOperationException;
import com.imooc.o2o.excptions.ExcelOperationException;
import com.imooc.o2o.excptions.ReachPointOperationException;
import com.imooc.o2o.service.ReachPointService;

@Service
public class ReachPointServiceImpl implements ReachPointService {
	
	@Autowired
	private ReachPointDao reachPointDao;
	@Override
	public List<ReachPoint> queryReachPointList(long userId) {
		return reachPointDao.queryReachPointList(userId);
	}
	@Override
	@Transactional
	public ReachPointExecution batchAddReachPoint(List<ReachPoint> reachPointList) throws ReachPointOperationException {
		if(reachPointList!=null&&reachPointList.size()>0) {
			try {
				int effectedNum=reachPointDao.batchInsertReachPoint(reachPointList);
				if(effectedNum<=0) {
					throw new ReachPointOperationException("达成度表单创建失败");
				}else {
					return new ReachPointExecution(ReachPointStateEnum.SUCCESS);
				}
			}catch (Exception e) {
				throw new ReachPointOperationException("bathAddReachPoint error:"+e.getMessage());
			}
		}else {
			return new ReachPointExecution(ReachPointStateEnum.EMPTY_LIST);
		}
	}
	@Override
	public ReachPointExecution deleteReachPoint(int reachPointId, long userId) throws ReachPointOperationException {
		try {
			int effectedNum=reachPointDao.deleteReachPoint(reachPointId,userId);
			if(effectedNum<=0) {
				throw new ReachPointOperationException("达成度表信息删除失败");
			}else {
				return new ReachPointExecution(ReachPointStateEnum.SUCCESS);
			}
		}catch (Exception e) {
			throw new ReachPointOperationException("deleteReachPoint error:"+e.getMessage());
		}
	}
	@Override
	public ReachPoint getReachPoint(int reachPointId) throws ExcelOperationException {
		return reachPointDao.queryByPointId(reachPointId);
	}
	@Override
	public ReachPointExecution modifyReachPoint(ReachPoint reachPoint) throws ExcelOperationException {
		if(reachPoint==null||reachPoint.getReachPointId()==null) {
			return new ReachPointExecution(ReachPointStateEnum.EMPTY_LIST);
		}else {
			try {
			int effectedNum=reachPointDao.updateReachPoint(reachPoint);
			if(effectedNum<=0) {
				return new ReachPointExecution(ReachPointStateEnum.INNER_ERROR);
			}else {
				reachPoint=reachPointDao.queryByPointId(reachPoint.getReachPointId());
				return new ReachPointExecution(ReachPointStateEnum.SUCCESS,reachPoint);
			}
			}catch (Exception e) {
				throw new ExcelOperationException("modifyReachPoint error"+e.getMessage());
			}
		}
	}
	@Override
	public ReachPoint getReachPointByExcelId(long excelId) {
		return reachPointDao.queryByExcelId(excelId);
	}
	@Override
	public ReachPointExecution modifyReachPointForScore(ReachPoint reachPoint) throws ExcelOperationException {
		if(reachPoint==null||reachPoint.getReachPointId()==null) {
			return new ReachPointExecution(ReachPointStateEnum.EMPTY_LIST);
		}else {
			try {
			int effectedNum=reachPointDao.updateReachPoint(reachPoint);
			if(effectedNum<=0) {
				return new ReachPointExecution(ReachPointStateEnum.INNER_ERROR);
			}else {
				reachPoint=reachPointDao.queryByPointId(reachPoint.getReachPointId());
				return new ReachPointExecution(ReachPointStateEnum.SUCCESS,reachPoint);
			}
			}catch (Exception e) {
				throw new ExcelOperationException("modifyReachPoint error"+e.getMessage());
			}
		}
	}
}
